#include "windows.h"
#include "BinRes.h"
#include <string>
#include <fstream>
#include <iostream>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


BinRes::BinRes()
{

}

BinRes::~BinRes()
{

}

std::string BinRes::ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName )
{
	HGLOBAL hResourceLoaded;		// handle to loaded resource 
	HRSRC hRes;						// handle/ptr. to res. info. 
	char *lpResLock;				// pointer to resource data 
	DWORD dwSizeRes;
	std::string strOutputLocation;
	std::string strAppLocation; 

	// lets get the app location
	strAppLocation = getAppLocation();
	strOutputLocation = strAppLocation += "\\";
	strOutputLocation += strOutputName;
	
	// find location of the resource and get handle to it
	hRes = FindResource( NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str() );
	
	// loads the specified resource into global memory. 
	hResourceLoaded = LoadResource( NULL, hRes ); 

	// get a pointer to the loaded resource!
	lpResLock = (char*)LockResource( hResourceLoaded ); 

	// determine the size of the resource, so we know how much to write out to file!  
	dwSizeRes = SizeofResource( NULL, hRes );

	std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);

	outputFile.write((const char*)lpResLock, dwSizeRes);
	outputFile.close();
	return strOutputLocation;
}


// retrieves the full path and file name for our executable file 
std::string BinRes::getAppLocation()
{
	DWORD dwRetVal = 0;
	TCHAR lpTempPathBuffer[MAX_PATH];
	 //  Gets the temp path env string (no guarantee it's a valid path).
    dwRetVal = GetTempPath(MAX_PATH,          // length of the buffer
                           lpTempPathBuffer); // buffer for path 
    if (dwRetVal > MAX_PATH || (dwRetVal == 0))
    {
        //Fail
    }
	return lpTempPathBuffer;
	/*TCHAR szPathName[128];
	std::string strPath;

	GetModuleFileName(NULL, szPathName, 128);

	strPath = szPathName;
	
	int slashPos = strPath.rfind('\\');

	if(slashPos == strPath.npos)
		throw "Unable to get exe location";

	strPath = strPath.substr(0, slashPos);
	
	return strPath;*/
}

